package tester;

/**
 * Created by IntelliJ IDEA.
 * User: aleck
 * Date: 2007-9-20
 * Time: 12:08:45
 * To change this template use File | Settings | File Templates.
 */
public class TestCaseResult {
  public static String exitCodeToString(TestCaseResult tcr) {
    if (tcr.exitCode == ExitCode.ERROR)
      return "E";
    else if (tcr.exitCode == ExitCode.TIME_OUT)
      return "O";
    else if (tcr.score == 100)
      return "T";
    else
      return "F";
  }

  public static ExitCode stringToExitCode(String s) {
    if (s.equals("E"))
      return ExitCode.ERROR;
    else if (s.equals("O"))
      return ExitCode.TIME_OUT;
    else
      return ExitCode.NORMAL;
  }

  public static enum ExitCode {
    TIME_OUT,
    NORMAL,
    ERROR,
  }

  public static char getExitCodeShortcut(TestCaseResult result) {
    if (result.exitCode == TestCaseResult.ExitCode.ERROR)
      return 'E';
    else if (result.exitCode == TestCaseResult.ExitCode.TIME_OUT)
      return 'O';
    else if (result.exitCode == TestCaseResult.ExitCode.NORMAL) {
      if (result.score == 100) return 'T';
      else return 'F';
    }
    // the last line should not be reached.
    return '.';
  }

  public final ExitCode exitCode;
  // when exitCode == TIME_OUT, timeUsed equals to the limited timeout.
  public final long timeUsed;
  public final int score;

  public TestCaseResult(ExitCode exitCode, long timeUsed, int score) {
    if (score < 0 || score > 100) throw new RuntimeException();
    this.score = score;
    this.timeUsed = timeUsed;
    this.exitCode = exitCode;
  }

  public String toString() {
    StringBuilder sb = new StringBuilder();
    if (exitCode == ExitCode.TIME_OUT) sb.append("TIME_OUT ");
    else if (exitCode == ExitCode.ERROR) sb.append("ERROR ");
    else if (exitCode == ExitCode.NORMAL) sb.append("NORMAL ");
    else sb.append("null ");
    sb.append("time: " + timeUsed + "ms ");
    sb.append("score: " + score);
    return sb.toString();
  }

}
